package com.jiazhong.shopping.servlets;

import com.jiazhong.shopping.dao.UserDao;
import com.jiazhong.shopping.dao.impl.UserDaoImpl;
import com.jiazhong.shopping.model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Servlet的功能是(三板斧):
 *  1.获得客户端请求
 *  2.调用处理类中的处理方法，进行功能处理
 *  3.根据处理结果进行页面跳转
 */
@WebServlet("/user.do")
public class UserServlet extends BaseServlet {
    private UserDao userDao = new UserDaoImpl();
    /**
     * 用户登录Servlet方法
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得login.jsp页面提交的账户名和密码
        String user_name = request.getParameter("user_name");
        String user_password = request.getParameter("user_password");

        //调用底层处理类进行功能处理
        //创建UserDao对象

        User user = userDao.login(user_name,user_password);
        if(user == null){//登录失败，用户名或密码错误
            request.setAttribute("errMSG","用户名或密码错误!");
            //请求转发，它不旦可以实现页面的跳转还可以将request对象传递到目标位置
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }else{
            //将登录者信息存入到session中,session也是一个作用域对象，该作用域比request长
            //获得session,session属于HttpSession接口的一个实现类对象
            HttpSession session = request.getSession();
            //将user对象存入到session中
            session.setAttribute("user",user);
            //重定向，重定向和请求转发一样都可以实现页面跳转，但重定向不能传递request对象
            response.sendRedirect("index.jsp");
        }

    }

    /**
     * 用户退出登录
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void exit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得session
        HttpSession session = request.getSession();
        //从session移除一个属性
        session.removeAttribute("user");
        //session.invalidate();//销毁session
        //重定向到index.jsp页面
        response.sendRedirect("index.jsp");
    }


    /**
     * 用户注册（向数据库的tbl_user表中添加用户）
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获得用户提交的数据
        String user_name = request.getParameter("user_name");
        String user_password = request.getParameter("user_password");
        String user_email = request.getParameter("user_email");


        //将获得的数据封装到User对象中
        User user = new User();
        user.setUser_name(user_name);
        user.setUser_password(user_password);
        user.setUser_email(user_email);
        request.getSession().setAttribute("user",user);


        //调用底层处理类进行功能处理
        userDao.addUser(user);
        //根据处理结果进行页面跳转
        //response.sendRedirect("index.jsp");
        //设置响应的内容类型
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print("<script>alert('注册成功!');location.href='index.jsp';</script>");
    }
}
